package com.eascs.web.o2o.scheduler.dao;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Repository;

import com.eascs.app.dao.JdbcSession;
import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;

@Repository
public class CrmDao {
	@Inject
	JdbcSession jdbcSessionForCrm;
	@Inject
	JdbcSession jdbcSessionForOracle;

	/**
	 * 统计表中数量
	 * 
	 * @param sql
	 * @return
	 */
	public Integer count(String sql, Object... params) {
		return this.jdbcSessionForOracle.findInteger(sql, params);

	}

	public <T> T getEntity(String sql, Class<T> requiredType, Object... params) {

		return this.jdbcSessionForOracle.findEntity(sql, requiredType, params);

	}

	public <T> T getEntityForCrm(String sql, Class<T> requiredType, Object... params) {

		return this.jdbcSessionForCrm.findEntity(sql, requiredType, params);

	}

	/**
	 * 分页查询
	 * 
	 * @param sqlPageQuery
	 * @return
	 */
	public <T> PageResult<T> findDataWithPage(SqlPageQuery<T> sqlPageQuery) {
		return this.jdbcSessionForOracle.findDataWithPage(sqlPageQuery);
	}

	/**
	 * 分页查询
	 * 
	 * @param sqlPageQuery
	 * @return
	 */
	public <T> PageResult<T> findDataWithPageForMysql(SqlPageQuery<T> sqlPageQuery) {
		return this.jdbcSessionForCrm.findDataWithPage(sqlPageQuery);
	}

	public List<Object[]> findParams(String sql, Object... params) {
		return this.jdbcSessionForCrm.findObjectsList(sql, params);
	}

	/**
	 * s 保存该表数据
	 */
	// @Transactional(transactionManager = "transactionManagerDbForCrm")
	public void saveEntity(Object entity) {
		jdbcSessionForCrm.insert(entity);
	}

	/**
	 * 删除数据
	 */
	public void deleteEntity(String delSql) {
		jdbcSessionForCrm.execute(delSql);
	}

	/**
	 * @param sql
	 * 
	 */
	// @Transactional(transactionManager = "transactionManagerDbForCrm")
	public void excuteSql(String sql, Object... params) {

		jdbcSessionForCrm.execute(sql, params);

	}

	public <T> List<T> findSimpleObjectList(String sql, Class<T> requiredType, Object... params) {
		return jdbcSessionForCrm.findSimpleObjectList(sql, requiredType, params);
	}

	public <T> T findById(Class<T> entityClass, String id) {
		return jdbcSessionForCrm.getById(entityClass, id);
	}

	public Integer countMysql(String sql, Object... params) {
		return this.jdbcSessionForCrm.findInteger(sql, params);

	}

	// @Transactional(transactionManager = "transactionManagerDbForCrm")
	public void updateEntity(Object entity) {
		jdbcSessionForCrm.updateById(entity);
	}

	public <T> List<T> findEntitysForMysql(String sql, Class<T> requiredType, Object... params) {
		return jdbcSessionForCrm.findEntitys(sql, requiredType, params);
	}

	public List<Object[]> findObjectsList(String sql, Object... params) {
		return jdbcSessionForCrm.findObjectsList(sql, params);
	}
}
